package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.qiantu.api.entity.MessageBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.glide.GlideApp;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 状态数据列表
 */
public final class NoticeAdapter extends AppAdapter<MessageBean.ListDTO> {

    public NoticeAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final ImageView iconMessage;
        private final TextView tvTime;
        private final TextView tvTitle;
        private final TextView tvContent;

        private ViewHolder() {
            super(R.layout.item_notice);
            iconMessage = findViewById(R.id.icon_message);
            tvTitle = findViewById(R.id.tv_title);
            tvTime = findViewById(R.id.tv_time);
            tvContent = findViewById(R.id.tv_content);
        }

        @Override
        public void onBindView(int position) {
            int corner = getResources().getDimensionPixelSize(R.dimen.dp_16);
            MessageBean.ListDTO item = getItem(position);
            if(!TextUtils.isEmpty(item.getImage())){
                iconMessage.setVisibility(View.VISIBLE);
                GlideApp.with(getContext())
                        .load(item.getImage())
                        .transform(new MultiTransformation<>(new RoundedCorners(corner)))
                        .into(iconMessage);
            }else{
                iconMessage.setVisibility(View.GONE);
            }
            RecyclerView.LayoutParams rl = (RecyclerView.LayoutParams) itemView.getLayoutParams();
            if(position == 0){
                rl.topMargin = getResources().getDimensionPixelSize(R.dimen.dp_12);
                rl.bottomMargin = 0;
            }else if(position == getItemCount() - 1){
                rl.topMargin = getResources().getDimensionPixelSize(R.dimen.dp_8);
                rl.bottomMargin = getResources().getDimensionPixelSize(R.dimen.dp_12);
            }else{
                rl.topMargin = getResources().getDimensionPixelSize(R.dimen.dp_8);
                rl.bottomMargin = 0;
            }
            itemView.setLayoutParams(rl);
            tvTitle.setText(item.getTitle());
            String time = item.getTime();
            if(time != null && time.contains("/")){
                time = time.replaceAll("/", "-");
                if(time.length() > 16){
                    time = time.substring(0, time.lastIndexOf(":"));
                }
            }
            tvTime.setText(time);
            tvContent.setText(item.getContent());
        }
    }

    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }
}